
//
//  BHTabBarController.m
//  PropertyMananger
//
//  Created by bh on 16/12/20.
//  Copyright © 2016年 BH. All rights reserved.
//

#import "BHTabBarController.h"
#import "BHNavigationController.h"

#import "HomeViewController.h"
#import "WorkViewController.h"
#import "QueryViewController.h"
#import "MineViewController.h"

#import "Tabbar.h"

@interface BHTabBarController ()<TabbarDelegate>

@property(nonatomic, weak) Tabbar * customTabbar;

@end

@implementation BHTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    // 设置所有UITabBarItem的文字属性
   [self setupItemTitleTextAttrs];
    
    //添加控制器
    [self setTabar];
    
    [self setChildController];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    for (UIView * child in self.tabBar.subviews)
    {
        // 删除系统自动生成的tabbar
        if ([child isKindOfClass:[UIControl class]])
        {
            [child removeFromSuperview];
        }
    }
    
}

/**
 * 加载tabbar
 */
-(void)setTabar
{
    Tabbar * customTabbar = [[Tabbar alloc]init];
    customTabbar.delegate = self;
    customTabbar.frame = self.tabBar.frame;
    [self.view addSubview:customTabbar];
    self.customTabbar = customTabbar;
    
}

/**
 * 实现代理方法
 */

-(void)tabBar:(Tabbar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to
{
    self.selectedIndex = to;
}

- (void)setupItemTitleTextAttrs
{
    // 设置normal状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    normalAttrs[NSForegroundColorAttributeName] =[UIColor whiteColor];
    
    // 设置selected状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = BHColor(255, 255, 0);
    
    // 利用Appearance对象统一设置文字属性
    UITabBarItem *item = [UITabBarItem appearance];
    
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}
#pragma mark - 添加控制器
/**
 * 初始化自控制器
 */
-(void)setChildController
{
    // 首页
    HomeViewController * homeVC = [[HomeViewController alloc] init];
   // homeVC.tabBarItem.badgeValue = @"12";
    [self tabbarWithController:homeVC withName:@"首页" withImageName:@"tab_home_unselected" withSelectedImageName:@"tab_home_onselected"];
    
    // 工单
    WorkViewController * workVC = [[WorkViewController alloc]init];
    workVC.tabBarItem.badgeValue = @"";
    [self tabbarWithController:workVC withName:@"工单" withImageName:@"tab_work_order_unselected" withSelectedImageName:@"tab_work_order_onselected"];
    
    // 消息
    QueryViewController * queryVC = [[QueryViewController alloc] init];
    queryVC.tabBarItem.badgeValue = @"new";
    [self tabbarWithController:queryVC withName:@"查询" withImageName:@"tab_query_unselected" withSelectedImageName:@"tab_query_onselected"];
    
    // 我
    MineViewController * mineVC = [[MineViewController alloc]init];
  //  mineVC.tabBarItem.badgeValue = @"100";
    [self tabbarWithController:mineVC withName:@"我的" withImageName:@"tab_my_unselected" withSelectedImageName:@"tab_my_onselected"];
}
/**
 * 创建tabbar
 */
-(void)tabbarWithController:(UIViewController*)viewCon withName:(NSString *)name withImageName:(NSString *)imageName withSelectedImageName:(NSString *)selectedImageName
{
    // 设置控制器的属性
    viewCon.title = name;
    
    
    // 设置控制器的图片
    viewCon.tabBarItem.image = [UIImage imageWithNamed:imageName];
    viewCon.tabBarItem.selectedImage = [[UIImage imageWithNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 包装控制器
    BHNavigationController * viewConNav = [[BHNavigationController alloc]initWithRootViewController:viewCon];
    [self addChildViewController:viewConNav];
    
    // 添加tabbar内部的按钮
    [self.customTabbar addTabBarWithItem:viewCon.tabBarItem];
}

@end
